package com.vnp.vchat.socket;

import java.net.MalformedURLException;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;

import org.json.JSONObject;

import android.util.Log;

import com.vnp.vchat.ApplicationContext;
import com.vnp.vchat.Util.PreferenceHelper;

public class MySocketIO implements IOCallback, IAppManager{
	private SocketIO socket;
	
	String URL = "http://w41.vnpgroup.net:8080";
	PreferenceHelper preferenceHelper = new PreferenceHelper(ApplicationContext.getContext());
	
	/*public void main(String[] arg1){
		try{
			new MySocketIO();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
	public void openSocket(){
		try {
			socket = new SocketIO(URL);
			socket.connect(this);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void join(){
		String dataJoin = preferenceHelper.getDataJoin();
		socket.emit("join", dataJoin);
	}
	
	/*public MySocketIO() throws Exception{
		socket = new SocketIO();
		socket.connect("http://w41.vnpgroup.net:8080", this);
		
	}*/

	@Override
	public void on(String event, IOAcknowledge ack, Object... args) {
         System.out.println("Server triggered event '" + event + "'");
         Object[] arguments = args;
     	String json = arguments[0].toString();
         if (event.equals("chat")){
         	Log.i("vChat", json.toString());
         }
             
	}

	@Override
	public void onConnect() {
		join();
	}

	@Override
	public void onDisconnect() {
		
	}

	@Override
	public void onError(SocketIOException arg0) {
		
	}

	@Override
	public void onMessage(String arg0, IOAcknowledge arg1) {
		
	}
	
	

	@Override
	public void onMessage(JSONObject arg0, IOAcknowledge arg1) {
		
	}
	


	@Override
	public void onReceiveMessage(String event, IOAcknowledge ack, Object... args) {
		
	}

}
